package gemini.castor.ui.client.page.content.account.accountinfo.form;

import gemini.basic.model.Address;
import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.address.AddressObject;
import gemini.castor.ui.client.page.widget.address.AddressWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class AccountInfoFormView extends Composite implements
		AccountInfoFormPresenter.Display {

	@UiTemplate("AccountInfoFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, AccountInfoFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;
	private AddressObject newAddressObject;

	public AccountInfoFormView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}

	@UiField
	HTML htmDistributorIDInfo;

	@UiField
	HTML htmFullName;

	@UiField
	HTML htmEmail;

	@UiField
	HTML htmPhone;

	@UiField
	HTML htmAddressError;

	@UiField
	HTML htmPasswordError;

	@UiField
	HTML htmEmailError;

	@UiField
	HTML htmPhoneError;

	@UiField
	PasswordTextBox tbxCurrentPassword;

	@UiField
	PasswordTextBox tbxEmailPassword;

	@UiField
	PasswordTextBox tbxPhonePassword;

	@UiField
	PasswordTextBox tbxAddressPassword;

	@UiField
	PasswordTextBox tbxNewPassword;

	@UiField
	PasswordTextBox tbxRetypeNewPassword;

	@UiField
	TextBox tbxRetypeNewEmail;

	@UiField
	TextBox tbxNewEmail;

	@UiField
	TextBox tbxRetypeNewPhone;

	@UiField
	TextBox tbxNewPhone;

	@UiField
	HTML htmAddress;

	@UiField
	HTML htmMaritalStatus;

	@UiField
	HTML htmUserName;

	@UiField
	HTML htmSex;

	@UiField
	HTML htmBirthday;

	@UiField
	HTML htmID;

	@UiField
	HTML htmHusbandWifeFullNameInfo;

	@UiField
	HTML htmHusbandWifeBirthday;

	@UiField
	HTML htmHusbandWifeID;

	@UiField
	HTML htmHoneyDateOfIssue;

	@UiField
	HTML htmHoneyPlaceOfIssue;

	@UiField
	HTML htmDateOfIssue;

	@UiField
	HTML htmPlaceOfIssue;

	@UiField
	VerticalPanel pnlHusbandWife;

	@UiField
	Button btnSave;

	@UiField
	Button btnSaveEmail;

	@UiField
	Button btnSaveAddress;

	@UiField
	Button btnSavePhone;

	@UiField
	AddressWidget awgAddress;

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void loadData() {
		newAddressObject = new AddressObject();
		awgAddress.setAddressObject(newAddressObject);
		
		reset();
		
		htmAddressError.setText(constants.stringRequireOnField());
		htmDistributorIDInfo.setText(model.getId());
		htmFullName.setText(model.getFullname());
		htmEmail.setText(model.getEmail());
		htmPhone.setText(model.getPhone());
		htmAddress.setText(model.getAddress());
		htmUserName.setHTML("<b>" + model.getUsername() + "</b>");
		htmSex.setText((model.getSex().contains("1")) ? constants.htmMale()
				: constants.htmFemale());
		htmBirthday.setText(model.getBirthday());
		htmID.setText(model.getIdcard());
		htmDateOfIssue.setText(model.getDateOfIssue());
		htmPlaceOfIssue.setText(model.getPlaceOfIssue());
		if (model.getMarital()) {
			htmMaritalStatus.setText(constants.htmMarried());
			htmHusbandWifeFullNameInfo.setText(model.getHoneyFullname());
			htmHusbandWifeBirthday.setText(model.getHoneyBirthday());
			htmHusbandWifeID.setText(model.getHoneyId());
			htmHoneyDateOfIssue.setText(model.getHoneyDateOfIssue());
			htmHoneyPlaceOfIssue.setText(model.getHoneyPlaceOfIssue());
			pnlHusbandWife.setVisible(true);
		} else {
			pnlHusbandWife.setVisible(false);
			htmMaritalStatus.setText(constants.htmSingle());
		}
	}

	private AccountInfoFormModel model;
	
	private void reset(){
		tbxAddressPassword.setValue("");
		tbxCurrentPassword.setValue("");
		tbxEmailPassword.setValue("");
		tbxNewEmail.setValue("");
		tbxNewPassword.setValue("");
		tbxNewPhone.setValue("");
		tbxPhonePassword.setValue("");
		tbxRetypeNewEmail.setValue("");
		tbxRetypeNewPassword.setValue("");
		tbxRetypeNewPhone.setValue("");

		awgAddress.reset();
	}

	@Override
	public void setModel(AccountInfoFormModel model) {
		this.model = model;
	}

	@Override
	public HasClickHandlers getChangePhoneClickHandlers() {
		return btnSavePhone;
	}

	@Override
	public boolean phoneValidate() {
		boolean result = false;

		tbxNewPhone.removeStyleDependentName("error");
		tbxRetypeNewPhone.removeStyleDependentName("error");
		tbxPhonePassword.removeStyleDependentName("error");
		htmPhoneError.setVisible(false);
		if (tbxNewPhone.getValue().isEmpty()) {
			showError(tbxNewPhone.getName(), constants.emptyField());
		} else if (tbxRetypeNewPhone.getValue().isEmpty()) {
			showError(tbxRetypeNewPhone.getName(), constants.emptyField());
		} else if (tbxPhonePassword.getValue().isEmpty()) {
			showError(tbxPhonePassword.getName(), constants.emptyField());
		} else if (!tbxNewPhone.getValue().equals(tbxRetypeNewPhone.getValue())) {
			showError(tbxNewPhone.getName(), constants.retypeNotEquals());
			showError(tbxRetypeNewPhone.getName(), constants.retypeNotEquals());
		} else {
			result = true;
		}

		return result;
	}

	@Override
	public boolean emailValidate() {
		boolean result = false;

		tbxNewEmail.removeStyleDependentName("error");
		tbxRetypeNewEmail.removeStyleDependentName("error");
		tbxEmailPassword.removeStyleDependentName("error");
		htmEmailError.setVisible(false);
		if (tbxNewEmail.getValue().isEmpty()) {
			showError(tbxNewEmail.getName(), constants.emptyField());
		} else if (tbxRetypeNewEmail.getValue().isEmpty()) {
			showError(tbxRetypeNewEmail.getName(), constants.emptyField());
		} else if (tbxEmailPassword.getValue().isEmpty()) {
			showError(tbxEmailPassword.getName(), constants.emptyField());
		} else if (!tbxNewEmail.getValue().equals(tbxRetypeNewEmail.getValue())) {
			showError(tbxNewEmail.getName(), constants.retypeNotEquals());
			showError(tbxRetypeNewEmail.getName(), constants.retypeNotEquals());
		} else {
			result = true;
		}

		return result;
	}

	@Override
	public boolean passwordValidate() {
		boolean result = false;
		tbxNewPassword.removeStyleDependentName("error");
		tbxRetypeNewPassword.removeStyleDependentName("error");
		tbxCurrentPassword.removeStyleDependentName("error");
		htmPasswordError.setVisible(false);
		if (tbxNewPassword.getValue().isEmpty()) {
			showError(tbxNewPassword.getName(), constants.emptyField());
		} else if (tbxRetypeNewPassword.getValue().isEmpty()) {
			showError(tbxRetypeNewPassword.getName(), constants.emptyField());
		} else if (tbxCurrentPassword.getValue().isEmpty()) {
			showError(tbxCurrentPassword.getName(), constants.emptyField());
		} else if (!tbxNewPassword.getValue().equals(
				tbxRetypeNewPassword.getValue())) {
			showError(tbxNewPassword.getName(), constants.retypeNotEquals());
			showError(tbxRetypeNewPassword.getName(),
					constants.retypeNotEquals());
		}else {
			result = true;
		}

		return result;
	}

	@Override
	public boolean addressValidate() {
		boolean result = false;

		tbxAddressPassword.removeStyleDependentName("error");
		htmPasswordError.setVisible(false);
		
		if (!awgAddress.isValidate()) {
			showError("", constants.emptyField());
		} else if (tbxAddressPassword.getValue().isEmpty()) {
			showError(tbxAddressPassword.getName(),
					constants.emptyField());
		}  else {
			result = true;
		}

		return result;
	}

	private void showError(String widgetName, String error) {
		if ("tbxCurrentPassword".equals(widgetName)) {
			tbxCurrentPassword.addStyleDependentName("error");
			htmPasswordError.setHTML(error);
			htmPasswordError.setVisible(true);
		} else if ("tbxEmailPassword".equals(widgetName)) {
			tbxEmailPassword.addStyleDependentName("error");
			htmEmailError.setHTML(error);
			htmEmailError.setVisible(true);
		} else if ("tbxPhonePassword".equals(widgetName)) {
			tbxPhonePassword.addStyleDependentName("error");
			htmPhoneError.setHTML(error);
			htmPhoneError.setVisible(true);
		} else if ("tbxAddressPassword".equals(widgetName)) {
			tbxAddressPassword.addStyleDependentName("error");
			htmAddressError.setHTML(error);
			htmAddressError.setVisible(true);
		} else if ("tbxNewPassword".equals(widgetName)) {
			tbxNewPassword.addStyleDependentName("error");
			htmPasswordError.setHTML(error);
			htmPasswordError.setVisible(true);
		} else if ("tbxRetypeNewPassword".equals(widgetName)) {
			tbxRetypeNewPassword.addStyleDependentName("error");
			htmPasswordError.setHTML(error);
			htmPasswordError.setVisible(true);
		} else if ("tbxRetypeNewEmail".equals(widgetName)) {
			tbxRetypeNewEmail.addStyleDependentName("error");
			htmEmailError.setHTML(error);
			htmEmailError.setVisible(true);
		} else if ("tbxNewEmail".equals(widgetName)) {
			tbxNewEmail.addStyleDependentName("error");
			htmEmailError.setHTML(error);
			htmEmailError.setVisible(true);
		} else if ("tbxRetypeNewPhone".equals(widgetName)) {
			tbxRetypeNewPhone.addStyleDependentName("error");
			htmPhoneError.setHTML(error);
			htmPhoneError.setVisible(true);
		} else if ("tbxNewPhone".equals(widgetName)) {
			tbxNewPhone.addStyleDependentName("error");
			htmPhoneError.setHTML(error);
			htmPhoneError.setVisible(true);
		} else {
			htmAddressError.setHTML(error);
			htmAddressError.setVisible(true);
		}
	}

	@Override
	public HasClickHandlers getChangeEmailClickHandlers() {
		return btnSaveEmail;
	}

	@Override
	public HasClickHandlers getChangeAddressClickHandlers() {
		return btnSaveAddress;
	}

	@Override
	public HasClickHandlers getChangePasswordClickHandlers() {
		return btnSave;
	}

	@Override
	public String getUsername() {
		return model.getUsername();
	}

	@Override
	public String getNewPhone() {
		htmPhoneError.setVisible(false);
		return tbxNewPhone.getText();
	}

	@Override
	public String getNewEmail() {
		htmEmailError.setVisible(false);
		return tbxNewEmail.getText();
	}

	@Override
	public String getNewPassword() {
		htmPasswordError.setVisible(false);
		return tbxNewPassword.getText();
	}

	@Override
	public Address getNewAddress() {

		Address newAddress = new Address();
		newAddress.setAddressLine1(newAddressObject.getLine1());
		newAddress.setAddressLine2(newAddressObject.getLine2());
		newAddress.setCity(newAddressObject.getCity());
		newAddress.setCountry(newAddressObject.getCountry());
		newAddress.setDistrict(newAddressObject.getDistrict());
		htmAddressError.setVisible(false);
		return newAddress;
	}
	
	@Override
	public String getConfirmPhonePassword(){
		return tbxPhonePassword.getText();
	}
	@Override
	public String getConfirmEmailPassword(){
		return tbxEmailPassword.getText();
	}
	@Override
	public String getConfirmAddressPassword(){
		return tbxAddressPassword.getText();
	}
	@Override
	public String getConfirmPassword(){
		return tbxCurrentPassword.getText();
	}
	
	@Override
	public String getStringPasswordNotMath(){
		return constants.passwordNotMatch();
	}
	@Override
	public String getStringEmailChanged(){
		return constants.emailChanged();
	}
	@Override
	public String getStringPhoneChanged(){
		return constants.phoneChanged();
	}
	@Override
	public String getStringAddressChanged(){
		return constants.addressChanged();
	}
	@Override
	public String getStringPasswordChanged(){
		return constants.passwordChanged();
	}
}
